package com.example.demo.controller;

import com.example.demo.pojo.AttenRecorder;
import com.example.demo.pojo.Rewar_Punish;
import com.example.demo.pojo.TrainingRecorder;
import com.example.demo.service.RecorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import java.util.Date;

@Controller
public class RecorderController {
    @Autowired
    private RecorderService recorderService;

    @RequestMapping("/atts")
    public String list_att(Model model){
        model.addAttribute("atts",recorderService.getAllAtt());
        return "recorder/attend/att_list";
    }

    @GetMapping("/addAtt")
    public String toAddAtt(){ return "recorder/attend/add";}

    @PostMapping("/att")
    public String addAtt(AttenRecorder attenRecorder){
        recorderService.addNemAtt(attenRecorder);
        return "redirect:/atts";
    }

    @GetMapping("/att/{id}")
    public String toUpdateAtt(@PathVariable("id")Integer id, Model model){
        model.addAttribute("att",recorderService.getAttById(id));
        return "recorder/attend/update";
    }

    @PostMapping("/updateatt")
    public String updateAtt(AttenRecorder attenRecorder){
        recorderService.updateAtt(attenRecorder);
        return "redirect:/atts";
    }

    @GetMapping("/delatt/{id}")
    public String deleteAtt(@PathVariable("id")Integer id){
        recorderService.deleteAtt(id);
        return "redirect:/atts";
    }


    @RequestMapping("/res")
    public String list_re(Model model){
        model.addAttribute("res",recorderService.getAllR_P());
        return "recorder/reward_punish/re_list";
    }

    @GetMapping("/addRe")
    public String toAddRe(){ return "recorder/reward_punish/add";}

    @PostMapping("/re")
    public String addRe(Rewar_Punish rewar_punish){
        recorderService.addNewR_P(rewar_punish);
        return "redirect:/res";
    }

    @GetMapping("/re/{id}")
    public String toUpdateRe(@PathVariable("id")Integer id, Model model){
        model.addAttribute("re",recorderService.getReById(id));
        return "recorder/reward_punish/update";
    }

    @PostMapping("/updatere")
    public String updateRe(Rewar_Punish rewar_punish){
        recorderService.updateRe(rewar_punish);
        return "redirect:/res";
    }

    @GetMapping("/delre/{id}")
    public String deleteRe(@PathVariable("id")Integer id){
        recorderService.deleteRe(id);
        return "redirect:/res";
    }


    @RequestMapping("/trs")
    public String list_tr(Model model){
        model.addAttribute("trs",recorderService.getAllTra());
        return "recorder/training/tr_list";
    }

    @GetMapping("/addtr")
    public String toAddTr(){ return "recorder/training/add";}

    @PostMapping("/tr")
    public String addTr(TrainingRecorder trainingRecorder){
        recorderService.addNewTra(trainingRecorder);
        return "redirect:/trs";
    }

    @GetMapping("/tr/{id}")
    public String toUpdateTr(@PathVariable("id")Integer id, Model model){
        model.addAttribute("tr",recorderService.getTraById(id));
        return "recorder/training/update";
    }

    @PostMapping("/updatetr")
    public String updateTr(TrainingRecorder trainingRecorder){
        recorderService.updateTra(trainingRecorder);
        return "redirect:/trs";
    }

    @GetMapping("/deltr/{id}")
    public String deleteTr(@PathVariable("id")Integer id){
        recorderService.deleteTra(id);
        return "redirect:/trs";
    }
    @PostMapping("/searchRecorderTraining")
    public ModelAndView searchTrainingRecorderByCondition(@RequestParam("conditionText") String conditionText,
                                                          @RequestParam("conditionType") String conditionType,
                                                            Model model
//                                                          @RequestParam("startTime") Date startTime,
//                                                          @RequestParam("endTime") Date endTime
    ){
        ModelAndView mv = new ModelAndView("recorder/training/tr_list");
        mv.addObject("trs", recorderService.getByConditions(conditionText,conditionType,null,null));
        model.addAttribute("text",conditionText);
        return mv;
    }

    @PostMapping("/searchReward")
    public ModelAndView searchAcc(@RequestParam("conditionText") String conditionText,
                                  @RequestParam("conditionType") String conditionType, Model model){
        ModelAndView mv = new ModelAndView("recorder/reward_punish/re_list");
        mv.addObject("res",recorderService.getRewardsByConditions(conditionText,conditionType));
        model.addAttribute("text",conditionText);
        return mv;
    }
}
